﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;

namespace RoadTeam.Pub
{
    public class Returnfactory
    {
        public static ForeachControlBase factory(Type T)
        {
            ForeachControlBase ForeachControlBase;
            switch (T.ToString().ToUpper())
            {
                case "SYSTEM.DATETIME":
                    ForeachControlBase = new ReturnDate();
                    break;
                case "SYSTEM.STRING":
                    ForeachControlBase = new ReturnString();
                    break;
                case "SYSTEM.BOOLEAN":
                    ForeachControlBase = new ReturnBool();
                    break;
                case "SYSTEM.INT32":
                    ForeachControlBase = new ReturnInt32();
                    break;
                case "SYSTEM.SINGLE":
                    ForeachControlBase = new ReturnSingle();
                    break;
                case "SYSTEM.DOUBLE":
                    ForeachControlBase = new ReturnDouble();
                    break;
                case "SYSTEM.DECIMAL":
                    ForeachControlBase = new ReturnDecimal();
                    break;
                default:
                    ForeachControlBase = new ForeachControlBase();
                    break;
            }
            return ForeachControlBase;
        }
    }
    public class ForeachControlBase
    {
        public virtual void ForeachControl(String constrolname, Control contr, ref object value)
        {
            value = null;
        }
    }
    class ReturnBool : ForeachControlBase
    {
        public override void ForeachControl(String constrolname, Control contr, ref object value)
        {
            String StrValue = String.Empty;
            ForEachControl.GetConTrolVal(ref StrValue, constrolname, contr);
            bool flag = false;
            bool.TryParse(StrValue, out flag);
            value = false;
        }
    }
    class ReturnDate : ForeachControlBase
    {
        public override void ForeachControl(String constrolname, Control contr, ref object value)
        {
            String StrValue = String.Empty;
            ForEachControl.GetConTrolVal(ref StrValue, constrolname, contr);
            DateTime dt = DateTime.MinValue;
            DateTime.TryParse(StrValue, out dt);
            if (dt == DateTime.MinValue) value = null;
            else value = Convert.ToDateTime(StrValue);
        }
    }
    class ReturnInt32 : ForeachControlBase
    {
        public override void ForeachControl(String constrolname, Control contr, ref object value)
        {
            String StrValue = String.Empty;
            ForEachControl.GetConTrolVal(ref StrValue, constrolname, contr);
            if (String.IsNullOrEmpty(StrValue)) value = null;
            int Num = int.MinValue;

            int.TryParse(StrValue, out Num);
            if (Num == int.MinValue) value = null;
            else value = Num;
        }
    }
    class ReturnString : ForeachControlBase
    {
        public override void ForeachControl(String constrolname, Control contr, ref object value)
        {
            String StrValue = String.Empty;
            ForEachControl.GetConTrolVal(ref StrValue, constrolname, contr);
            value = StrValue;
        }
    }
    class ReturnSingle : ForeachControlBase
    {
        public override void ForeachControl(String constrolname, Control contr, ref object value)
        {
            String StrValue = String.Empty;
            ForEachControl.GetConTrolVal(ref StrValue, constrolname, contr);
            if (String.IsNullOrEmpty(StrValue)) value = null;
            Single single = Single.MinValue;
            Single.TryParse(StrValue, out single);
            if (single == Single.MinValue) value = null;
            else value = single;
        }
    }
    class ReturnDouble : ForeachControlBase
    {
        public override void ForeachControl(String constrolname, Control contr, ref object value)
        {
            String StrValue = String.Empty;
            ForEachControl.GetConTrolVal(ref StrValue, constrolname, contr);
            if (String.IsNullOrEmpty(StrValue)) value = null;
            double doublevalue = double.MinValue;
            double.TryParse(StrValue, out doublevalue);
            if (doublevalue == double.MinValue) value = null;
            else value = doublevalue;
        }
    }
    class ReturnDecimal : ForeachControlBase
    {
        public override void ForeachControl(String constrolname, Control contr, ref object value)
        {
            String StrValue = String.Empty;
            ForEachControl.GetConTrolVal(ref StrValue, constrolname, contr);
            if (String.IsNullOrEmpty(StrValue)) value = null;
            decimal decimalvalue = decimal.MinValue;
            decimal.TryParse(StrValue, out decimalvalue);

            if (decimalvalue == decimal.MinValue) value = null;
            else value = decimalvalue;
        }
    }
}
